package com.controller;

import com.model.*;
import com.service.EmpService;
import com.service.RoleService;
import com.service.SectionService;
import com.util.MD5Util;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/9/1.
 */
@Controller
public class EmpController {
    @Resource
    private EmpService empService;
    @Resource
    private RoleService roleService;
    @Resource
    private SectionService sectionService;

    private MD5Util md5Util = new MD5Util();

    @RequestMapping("getEmpList.do")
    public String getEmpList(@RequestParam(value = "name",required = false)String name,
                             @RequestParam(value = "mobile",required = false)String mobile,
                             @RequestParam(value = "sectionid",required = false)Integer sectionid,
                             @RequestParam(value = "roleid",required = false)Integer roleid,
                             @RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                             HttpServletRequest request){
        if(mobile!=null&&mobile.equals("")){mobile=null;}
        Integer pagesize = 15;
        Integer limit = (page - 1) * pagesize;
        List<Emp> emps = empService.getEmpList(new Emp(name,mobile,sectionid,roleid,limit,pagesize));
        for (Emp emp:emps) {
            String rolename = roleService.getRoleNameById(new Role(emp.getRoleid()));
            String sectionname = sectionService.getSectionNameById(new Section(emp.getSectionid()));
            emp.setSectionname(sectionname);
            emp.setRolename(rolename);
        }
        int count = empService.countTotalOfEmp(new Emp(name,mobile,sectionid,roleid));
        Page page1 = new Page(page,15,count);
        List<Section> sectionlist = sectionService.getAllSections();
        List<Role> rolelist = roleService.getAllRoles();
        request.setAttribute("emps",emps);
        request.setAttribute("name",name);
        request.setAttribute("mobile",mobile);
        request.setAttribute("sectionid",sectionid);
        request.setAttribute("roleid",roleid);
        request.setAttribute("page",page1);
        request.setAttribute("sectionlist",sectionlist);
        request.setAttribute("rolelist",rolelist);
        return "emp";
    }

    @RequestMapping("insertEmp.do")
    public String insertEmp(Emp emp, HttpSession session, HttpServletRequest request){
        String manager = (String) session.getAttribute("sessionUsername");
        if(manager == null){
            return "login";
        }
        String creator = manager;
        Date date = new Date();
        Long createTime = date.getTime()/1000;
        emp.setCreatetime(createTime);
        emp.setCreator(creator);
        String id = md5Util.transMD5(date.getTime() + "pinchengcloud");
        emp.setId(id);
        int result = empService.insertEmp(emp);
        if(result==0){
            request.setAttribute("error","添加失败");
        }
        return getEmpList(null,null,null,null,1,request);
    }

    @RequestMapping("modifyEmp.do")
    public String modifyEmp(Emp emp, HttpServletRequest request){
        int result = empService.modifyEmp(emp);
        if(result==0){
            request.setAttribute("error","修改失败");
        }
        return getEmpList(null,null,null,null,1,request);
    }

    @RequestMapping("changeEmpStatus.do")
    public String changeEmpStatus(@RequestParam("id")String id,
                                   @RequestParam("status")Integer status,
                                   HttpServletRequest request){
        int result = empService.changeStatus(new Emp(id,status));
        if(result==0){
            request.setAttribute("error","修改失败");
        }
        return getEmpList(null,null,null,null,1,request);
    }

    @RequestMapping("deleteEmp.do")
    public String deleteEmp(@RequestParam("id")String id,
                             HttpServletRequest request){
        int result = empService.deleteEmp(new Emp(id));
        if(result==0){
            request.setAttribute("error","删除失败");
        }
        return getEmpList(null,null,null,null,1,request);
    }

    @RequestMapping("checkEmpMobileExist")
    @ResponseBody
    public Result checkEmpMobileExist(@RequestParam("mobile")String mobile){
        Emp emp = new Emp();
        emp.setMobile(mobile);
        int count = empService.checkExist(emp);
        return new Result(count,null,null);
    }

    @RequestMapping("selectEmp")
    @ResponseBody
    public Result selectEmp(@RequestParam(value = "sectionid",required = false)Integer sectionid,
                            @RequestParam(value = "roleid",required = false)Integer roleid){
        List<Emp> emps = empService.getEmpList(new Emp(sectionid,roleid));
        if(emps.size()==0){
            return new Result(0,"无数据",null);
        }
        for (Emp emp : emps){
            if(sectionid!=null) {
                String role = roleService.getRoleNameById(new Role(emp.getRoleid()));
                emp.setRolename(role);
            }
            if(roleid!=null){
                String section = sectionService.getSectionNameById(new Section(emp.getSectionid()));
                emp.setSectionname(section);
            }
        }
        return new Result(1,"查询成功",emps);
    }
}
